﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using TVC.Layout;
using Gizmox.WebGUI.Forms;

namespace TVC.Modules.CustomControl
{
    public class MultiColumnCombo : SearchTextBox
    {
        object _binding;
        string _lookup;
        GridLayout layout;

        public GridLayout Layout
        {
            get { return layout; }
            set { layout = value; }
        }
        public MainForm MainForm { get; set; }
        DataRow rowData;

        public DataRow RowData
        {
            get { return rowData; }
            set { rowData = value; }
        }
        DataRow[] rowDatas;

        public DataRow[] RowDatas
        {
            get { return rowDatas; }
            set { rowDatas = value; }
        }

        public string DisplayField { get; set; }
        public string ValueField { get; set; }
        public int DisplayColumn = -1;
        public int ValueColumn = -1;
        public delegate void ComboChanged(object sender, ComboChangedEventArgs e);

        public class ComboChangedEventArgs : System.EventArgs
        {
            private string strText;

            public ComboChangedEventArgs(string strText)
            {
                this.strText = strText;
            }

            public string mccbText
            {
                get
                {
                    return strText;
                }
            }

        }

        public event ComboChanged ComboText_Changed;

        Forms.frmView frm;
        public MultiColumnCombo()
        {
            CustomStyle = "";
        }
        public object GetProperty(string property)
        {
            if (rowData != null)
                return rowData[property];
            else return null;
        }
        public string Value
        {
            get
            {
                if (_binding != null)
                {
                    if (ValueField != null)
                    {
                        if (ValueField != null && rowData != null && rowData.Table.Columns.Contains(ValueField))
                            return rowData[ValueField].ToString();
                        else return "";
                    }
                    else
                    {
                        if (ValueColumn != -1 && rowData != null)
                            return rowData[ValueColumn].ToString();
                        else return "";
                    }
                }
                else return Text;

            }
            set
            {
                if (_binding != null)
                {
                    if (value != Value)
                    {
                        if (value == "" || value == null)
                        {
                            rowData = null;
                            Text = "";
                            return;
                        }
                        if (_binding != null)
                        {
                            using (DataTable dt = GetDataTable())
                            {
                                if (dt != null)
                                    SetValue(value, dt);
                            }
                        }
                    }
                }
                else Text = value;
            }
        }

        private DataTable GetDataTable()
        {
            object datasource = _binding as BindingSource;
            if (datasource == null)
            {
                datasource = _binding;
            }
            else
            {
                datasource = ((BindingSource)_binding).DataSource;
            }
            DataTable dt = null;
            if (datasource is DataTable)
                dt = datasource as DataTable;
            else if (datasource is DataView)
            {
                DataView dv = datasource as DataView;
                dt = dv.Table;
            }
            return dt;
        }
        public bool AutoScroll { get; set; }
        public int DisplayRows { get; set; }
        public bool Filter { get; set; }
        public bool KeyPressChk { get; set; }
        public AutoValidate AutoValidate { get; set; }
        private void SetValue(String value)
        {
            if (value != Value)
            {
                if (value == "" || value == null)
                {
                    rowData = null;
                    Text = "";
                    return;
                }
                if (_binding != null)
                {
                    using (DataTable dt = GetDataTable())
                    {
                        if (dt != null)
                            SetValue(value, dt);
                    }
                }
            }
        }

        private void SetValue(string value, DataTable dt)
        {
            bool flag = false;
            if (ValueField != null && dt.Columns.Contains(ValueField))
            {
                foreach (DataRow row in dt.Rows)
                {
                    if (row[ValueField].ToString() == value)
                    {
                        flag = true;
                        rowData = row;
                        Text = rowData[DisplayField].ToString();
                        if (ComboText_Changed != null)
                        {
                            rowDatas = null;
                            ComboText_Changed(this, new ComboChangedEventArgs(rowData[ValueField].ToString()));
                        } break;
                    }

                }
            }
            else if (ValueColumn != -1 && dt.Columns.Count > ValueColumn)
            {
                foreach (DataRow row in dt.Rows)
                {
                    if (row[ValueColumn].ToString() == value)
                    {
                        flag = true;
                        rowData = row;
                        Text = rowData[DisplayColumn].ToString();
                        if (ComboText_Changed != null)
                        {
                            rowDatas = null;
                            ComboText_Changed(this, new ComboChangedEventArgs(rowData[ValueColumn].ToString()));
                        } break;
                    }

                }
            }
            else
            { throw new Exception("The index is not valid"); }
            if (flag == false)
            {
                rowData = null;
                rowDatas = null;
                Text = "";
                if (ComboText_Changed != null)
                {
                    ComboText_Changed(this, new ComboChangedEventArgs(""));
                }
            }
        }
        public string Lookup
        {
            get { return _lookup; }
            set
            {
                if (value != null && _lookup != value)
                {
                    _lookup = value;
                    layout = new GridLayout(MainForm.ServerPath + "Resources\\Layout\\", String.Format("{0}Documents\\Layout\\{1}\\", MainForm.ServerPath, MainForm.PODInfo.USER_ID), _lookup);
                }
            }
        }
        public object Binding
        {
            get { return _binding; }
            set
            {
                if (_binding != value)
                {
                    frm = null;
                    _binding = value;
                }
                if (value != null)
                {
                    this.CustomStyle = "STB";
                    Search += new EventHandler(MultiColumnCombo_Search);
                    Click += MyTextBox_Click;
                    Validated += MultiColumnCombo_Validated;
                }
                else
                {
                    this.CustomStyle = "";
                }

            }
        }

        void MultiColumnCombo_Validated(object sender, EventArgs e)
        {
            if (Text == null || Text == "")
            {
                rowData = null;
                return;
            }
            if (_binding != null)
            {
                using (DataTable dt = GetDataTable())
                {
                    if (dt != null && ValueColumn >= 0)
                        SetText(dt);
                    else if (dt != null && ValueField != null)
                        SetText(dt);
                }
            }
            else
            {
                if (ComboText_Changed != null)
                {
                    rowDatas = null;
                    ComboText_Changed(this, new ComboChangedEventArgs(this.Text));
                }
            }
        }
        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
            if (e.KeyCode == Keys.Enter && this.Text == "")
            {
                OpenView();
            }
        }

        private void OpenView()
        {
            if (frm == null || frm.IsDisposed || !frm.Visible)
                Find();
            else if (!frm.Visible)
                frm.Visible = true;
            //frm.ShowDialog();
        }
        void MultiColumnCombo_Search(object sender, EventArgs e)
        {
            IsSearchActive = false;
            OpenView();
        }
        protected override void OnTextChanged(EventArgs e)
        {
            //MyTextBox_TextChanged(this, e);
        }

        void MyTextBox_TextChanged(object sender, EventArgs e)
        {
            if (Text == null || Text == "")
            {
                rowData = null;
                if (ComboText_Changed != null)
                {
                    rowDatas = null;
                    ComboText_Changed(this, new ComboChangedEventArgs(this.Text));
                }
                return;
            }
            if (_binding != null)
            {
                using (DataTable dt = GetDataTable())
                {
                    if (dt != null && ValueColumn >= 0)
                        SetText(dt);
                    else if (dt != null && ValueField != null)
                        SetText(dt);
                }
            }
            else if (ComboText_Changed != null)
            {
                rowDatas = null;
                ComboText_Changed(this, new ComboChangedEventArgs(this.Text));
            }
        }

        private void SetText(DataTable dt)
        {
            if (ValueField != null && dt.Columns.Contains(ValueField))
            {
                if (rowData != null && rowData[DisplayField].ToString() != Text)
                {
                    bool flag = false;
                    foreach (DataRow row in dt.Rows)
                    {
                        if (row[DisplayField].ToString() == Text)
                        {
                            rowData = row;
                            //this.Text = rowData[DisplayField].ToString();
                            if (ComboText_Changed != null)
                            {
                                rowDatas = null;
                                ComboText_Changed(this, new ComboChangedEventArgs(rowData[ValueField].ToString()));
                            }
                            flag = true;
                            break;
                        }
                    }
                    if (flag == false)
                    {
                        if (rowData != null)
                            this.Text = rowData[DisplayField].ToString();
                        else this.Text = "";
                    }
                }
            }
            else if (ValueColumn != -1 && dt.Columns.Count > ValueColumn)
            {
                if (rowData != null && rowData[DisplayColumn].ToString() != Text)
                {
                    bool flag = false;
                    foreach (DataRow row in dt.Rows)
                    {
                        if (row[DisplayColumn].ToString() == Text)
                        {
                            rowData = row;
                            //this.Text = rowData[DisplayField].ToString();
                            if (ComboText_Changed != null)
                            {
                                rowDatas = null;
                                ComboText_Changed(this, new ComboChangedEventArgs(rowData[ValueColumn].ToString()));
                            }
                            flag = true;
                            break;
                        }
                    }
                    if (flag == false)
                    {
                        if (rowData != null)
                            Text = rowData[DisplayColumn].ToString();
                        else Text = "";
                    }
                }
            }
            else throw new Exception("The index is not valid");
        }

        void MyTextBox_Click(object sender, EventArgs e)
        {
            if (Text == null || Text == "" && CustomStyle == "STB")
            {
                OpenView();
            }
        }

        //protected  void FireEvent(Gizmox.WebGUI.Common.Interfaces.IEvent objEvent)
        //{
        //    //base.FireEvent(objEvent);
        //    if (objEvent.Type == "Search")
        //    {
        //        if (frm == null || frm.IsDisposed)
        //            Find();
        //        else if (!frm.Visible)
        //            frm.Visible = true;

        //    }
        //    else if (objEvent.Type == "Click" && Text == "")
        //    {
        //        if (frm == null || frm.IsDisposed)
        //            Find();
        //        else if (!frm.Visible)
        //            frm.Visible = true;
        //    }
        //    else if (objEvent.Type == "Clear")
        //    {
        //    }
        //}

        public void Find()
        {
            if (_binding != null)
            {
                frm = new Forms.frmView(layout, MainForm);
                DataTable dt = GetDataTable();
                frm.DataSource = dt;
                frm.FormClosed += (form, args) =>
                {
                    if (((Forms.frmView)form).DialogResult == DialogResult.OK)
                    {
                        rowData = ((Forms.frmView)form).ReturnValue;
                        //rowDatas = null;
                        rowDatas = ((Forms.frmView)form).ReturnValues;
                        if (ValueField != null && rowData.Table.Columns.Contains(ValueField))
                        {
                            Text = rowData[DisplayField].ToString();
                            if (ComboText_Changed != null)
                            {

                                ComboText_Changed(this, new ComboChangedEventArgs(rowData[ValueField].ToString()));
                            }
                        }
                        if (ValueColumn != -1 && rowData.Table.Columns.Count > ValueColumn)
                        {
                            Text = rowData[DisplayColumn].ToString();
                            if (ComboText_Changed != null)
                            {
                                ComboText_Changed(this, new ComboChangedEventArgs(rowData[ValueColumn].ToString()));
                            }
                        }
                    }
                };
                frm.ShowDialog();

            }
        }

    }
}